<template>
  <div class="dashboard-editor-container">
    <!-- <github-corner class="github-corner" /> -->

    <panel-group
      v-if="data.length >0"
      :user-count="user_count"
      :city-count="city_count"
      :file-count="file_count"
      :size-count="size_count"
      @handleSetLineChartData="handleSetLineChartData"
    />

    <el-row :gutter="32" style="background:#fff;margin-bottom:32px;">
      <el-col :xs="48" :sm="48" :lg="16" style="padding:0">
        <div class="chart-wrapper">
          <!-- <div>位置分布</div> -->
          <MapChart v-if="data.length >0" :data="data" />
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <!-- <bar-chart /> -->
          <pie-chart
            v-if="data.length >0"
            :city-arr="city_arr"
            :city-name="city_name"
          />
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import GithubCorner from '@/components/GithubCorner'
import PanelGroup from './components/PanelGroup'
import LineChart from './components/LineChart'
import RaddarChart from './components/RaddarChart'
import PieChart from './components/PieChart'
import BarChart from './components/BarChart'
import TransactionTable from './components/TransactionTable'
import TodoList from './components/TodoList'
import BoxCard from './components/BoxCard'
import MapChart from './components/MapChart'
import { homecount } from '@/api/home'

const lineChartData = {
  newVisitis: {
    expectedData: [100, 120, 161, 134, 105, 160, 165],
    actualData: [120, 82, 91, 154, 162, 140, 145]
  },
  messages: {
    expectedData: [200, 192, 120, 144, 160, 130, 140],
    actualData: [180, 160, 151, 106, 145, 150, 130]
  },
  purchases: {
    expectedData: [80, 100, 121, 104, 105, 90, 100],
    actualData: [120, 90, 100, 138, 142, 130, 130]
  },
  shoppings: {
    expectedData: [130, 140, 141, 142, 145, 150, 160],
    actualData: [120, 82, 91, 154, 162, 140, 130]
  }
}

export default {
  name: 'DashboardAdmin',
  components: {
    GithubCorner,
    PanelGroup,
    LineChart,
    RaddarChart,
    PieChart,
    BarChart,
    TransactionTable,
    TodoList,
    BoxCard,
    MapChart
  },

  data() {
    return {
      lineChartData: lineChartData.newVisitis,
      data: [],
      user_count: 0,
      city_count: 0,
      file_count: 0,
      size_count: 0,
      city_arr: [],
      city_name: []

    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleSetLineChartData(type) {
      this.lineChartData = lineChartData[type]
    },
    getList() {
      homecount({}).then(response => {
        this.data = JSON.parse(JSON.stringify(response.data.map))
        this.user_count = response.data.user_count
        this.city_count = response.data.city_count
        this.file_count = response.data.file_count
        this.size_count = response.data.size_count
        this.soryList(this.data)
      }).catch(err => {
            console.log(err)
        })
    },
    soryList(list) {
      const arr = list.sort(this.sortBy('value'))
      const city_arr = []
      const city_name = []
      for (let i = 0; i < 10; i++) {
        city_arr.push(arr[i])
        city_name.push(arr[i]['name'])
      }
      this.city_arr = city_arr
      this.city_name = city_name
      // console.log(city_name)
    },
    sortBy(property) {
      return function(value1, value2) {
        const a = value1[property]
        const b = value2[property]
        return a < b ? 1 : a > b ? -1 : 0
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  position: relative;

  .github-corner {
    position: absolute;
    top: 0px;
    border: 0;
    right: 0;
  }

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width:1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
